import {
	post,
	get,
	postL,
  getL
} from '@/utils/http'
import {
	trans
} from './base'
import {
	SERVICE_URL,
	WX_APP_ID
} from '@/utils/config'
import store from '@/store'
import {
	SET_THIRD,
	SET_TOKEN,
	SET_USER_INFO,
	SET_HIDE
} from '@/store/mutation-types'
import cache from '../utils/cache'

export default {
	apis: {
		WX_SESSION: SERVICE_URL + '/api/wechatMa/getSessionInfo',
		Login: SERVICE_URL + '/api/wechatMa/loginV2',
		MOBILE_BIND: SERVICE_URL + '/api/wechatMa/bindUserPhone',
		USER_INFO: SERVICE_URL + '/api/myProfile/get',
		USER_UPDATE: SERVICE_URL + '/api/myProfile/basicInfo/update',
		UPDATE_AVATAR: SERVICE_URL + '/api/myProfile/avatar/update',
		UPDATE_NICKNAME: SERVICE_URL + '/api/myProfile/nickname/update',
		USER_COMPLETE_BIND: SERVICE_URL + '/api/wechatMa/bindUserInfo',

		REAL_NAME_APPLY: SERVICE_URL + '/api/authentication/submit',
		GETHIDE: SERVICE_URL + '/api/luckOrder/hide'
	},
	async updateSession(code, loading) {
		const res = await trans(post(this.apis.WX_SESSION, {
			code
		}, {
			loading
		}))
		if (res.data && res.data.session) {
			store.commit(SET_THIRD, res.data.session)
		}
		return res.success
	},
	async login(encryptedData, iv, session) {
		const res = await trans(
			postL(this.apis.Login, {
				encryptedData,
				iv,
				sessionKey: session.sessionKey,
				session
			})
		)
		if (!res.success) {
			return false
		}
		store.commit(SET_TOKEN, res.data.tokenInfo.tokenValue)
		return {
			needInfo: res.data.needPerfectProfile,
			token: res.data.tokenInfo.tokenValue
		}
	},
	async getInfo() {
		const res = await trans(get(this.apis.USER_INFO))

		return res.data
	},
	async info() {
		const res = await trans(get(this.apis.USER_INFO))
		if (res.success) {
			store.commit(SET_USER_INFO, res.data)
		}
		return res.success
	},
	async bindMobile(param) {
		param.sessionKey = store.state.sessionKey
		const res = await trans(postL(this.apis.MOBILE_BIND, param))
		return res.success
	},
	async complete(encryptedData, iv, signature, rawData) {
		let param = {
			encryptedData,
			iv,
			sessionKey: store.state.sessionKey,
			signature,
			rawData
		}
		const res = await trans(postL(this.apis.USER_COMPLETE_BIND, param))
		return res.success
	},
	async realNameApply(idName, idNum) {
		const res = await trans(postL(this.apis.REAL_NAME_APPLY, {
			idName,
			idNum
		}))
		return res.success
	},
	async updateInfo(param) {
		const res = await trans(postL(this.apis.USER_UPDATE, param))
		return res.success
	},
	async updateAvatar(avatar) {
		const res = await trans(postL(this.apis.UPDATE_AVATAR, {
			avatar
		}))
		return res.success
	},
	async updateNickname(nickname) {
		const res = await trans(postL(this.apis.UPDATE_NICKNAME, {
			nickname
		}))
		return res.success
	},
	async getHide() {
		const res = await trans(get(this.apis.GETHIDE))
		// 1: 没消费，0 ：消费
		store.commit(SET_HIDE, res.data)
		return res.success
	},
	async getVerifycode(param) {
		const res = await trans(postL(SERVICE_URL + '/api/verifycode/send', param))

		return res.success
	},
	async accountLogin(param) {
		const res = await trans(postL(SERVICE_URL + '/api/account/login', param))
		if (!res.success) {
			return false
		}
		store.commit(SET_TOKEN, res.data.tokenInfo.tokenValue)
		return res.success
	},
	async getParamConfig(code) {
		const res = await trans(get(SERVICE_URL + '/param/paramConfig', {
			code,
      loading:false
		}))
		// console.log('paramConfig：' + JSON.stringify(res))
		return res.data
	},
	async logoff() {
		const res = await trans(postL(SERVICE_URL + '/api/removeAccount/submit'))
		return res.success
	},
	async getWalletAmount(param) {
		const res = await trans(getL(SERVICE_URL + '/api/wallet/ranking/walletAmount',param))
		return res
	},
  async getNewUserNum(param) {
		const res = await trans(getL(SERVICE_URL + '/api/wallet/ranking/newUserNum',param))
		return res
	},
}
